import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Leetcode2032 {
    public List<Integer> twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
        return IntStream.concat(IntStream.concat(IntStream.of(nums1).distinct(), IntStream.of(nums2)
                        .distinct()), IntStream.of(nums3).distinct())
                .boxed()
                .collect(Collectors.groupingBy(Integer::intValue, Collectors.collectingAndThen(Collectors.toList(), List::size)))
                .entrySet()
                .stream()
                .filter(entry -> entry.getValue() > 1)
                .map(Map.Entry::getKey).collect(Collectors.toList());
    }
}
